package org.activequant.container.report;

import org.activequant.core.domainmodel.account.Account;
import org.activequant.dao.IAccountDao;
import org.apache.log4j.Logger;

/**
 *
 * A Reporting Service that saves a report and especially the account object
 * to the dao layer. Beware that this class will delete any existing account
 * object before from the dao layer in case it finds it, and then saves it
 * again. <br/>
 * A TextReportingService subclass. Holds the following associated variable:
 * <ul>
 * <li>theAccountDao(IAccountDao)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [22.05.2009] Created (Ulrich Staudinger)<br
 *
 *  @author Ulrich Staudinger
 */
public class DaoReportingService extends TextReportingService {
	
	private static Logger log = Logger.getLogger(DaoReportingService.class.getName());

    /**
     * renders the account to database.<br/>
     * finds and deletes any existing Account[] database records matching the Account of the given accountReport(AccountReport) and then updates/recreates them again
     * using the associated theAccountDao(IAccountDao).
     * @param accountReport
     */
	public void renderAccountReport(AccountReport accountReport) {
        // delete any old account at first ...
        Account[] myAccounts = theAccountDao.findAllByExample(accountReport.getAccount());
        if(myAccounts!=null)
        {
            for(Account myAccount : myAccounts)
                theAccountDao.delete(myAccount);
        }
        theAccountDao.update(accountReport.getAccount());        
    }


    /**
     * plain getter.<br/>
     * returns the associated theAccountDao(IAccountDao)
     * @return the used account dao. 
     */
    public IAccountDao getAccountDao()
    {
        return theAccountDao;
    }

    /**
     * plain setter.<br/>
     * sets the associated theAccountDao(IAccountDao) with the given anAccountDao(IAccountDao)
     * @param anAccountDao the account dao to use.
     */
    public void setAccountDao(IAccountDao anAccountDao)
    {
        theAccountDao = anAccountDao;
    }
    /**
     * private IAccountDao theAccountDao;
     */
    private IAccountDao theAccountDao;

}
